package logic

import (
	"context"

	"mall/service/attachment/rpc/internal/svc"
	"mall/service/attachment/rpc/internal/utils/errorhandler"
	"mall/service/attachment/rpc/types/attachment"
	"mall/service/backend/common/i18n"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackendDeleteAttachmentLogLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBackendDeleteAttachmentLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendDeleteAttachmentLogLogic {
	return &BackendDeleteAttachmentLogLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BackendDeleteAttachmentLogLogic) BackendDeleteAttachmentLog(in *attachment.IDsReq) (*attachment.BaseResp, error) {
	var ids []uint64
	for _, id := range in.Ids {
		if id > 0 {
			ids = append(ids, id)
		}
	}

	if len(ids) == 0 {
		return &attachment.BaseResp{
			Msg: i18n.DeleteSuccess,
		}, nil
	}

	tx, err := l.svcCtx.DB.BeginTx(l.ctx, nil)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	err = l.svcCtx.CurrentFileLog.ForceDeleteBatch(l.ctx, tx, ids)
	if err != nil {
		tx.Rollback()
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}
	tx.Commit()

	return &attachment.BaseResp{
		Msg: i18n.DeleteSuccess,
	}, nil
}
